﻿Imports Microsoft.DirectX.Direct3D
Imports Microsoft.DirectX
Imports Commander_Wars.Spieletools
Public MustInherit Class ObjektAnimation
    Protected Shared Texturen As Textur() = {}
    Structure Textur
        Public Sub New(ByVal Texturname As String, ByVal Ordner As String, ByVal Frames As Integer)
            Texturnamen = Texturname
            Dim B As Image = Nothing
            If Hauptmenue.ZoomedIn Then
                Dim Found As Boolean = False
                For Each File In My.Computer.FileSystem.GetFiles(DataSettings.Resourcenpfad + Ordner + "\Zoomed In\", FileIO.SearchOption.SearchAllSubDirectories, "*.png")
                    If File.ToLower.EndsWith((Texturname + ".png").ToLower) Then
                        B = Image.FromFile(File)
                        Found = True
                    End If
                Next
                If Not Found Then
                    If My.Computer.FileSystem.FileExists(DataSettings.Resourcenpfad + Ordner + "\Zoomed In\" + Texturname + ".png") Then
                        B = Image.FromFile(DataSettings.Resourcenpfad + Ordner + "\Zoomed In\" + Texturname + ".png")
                    Else
                        B = Image.FromFile(DataSettings.Resourcenpfad + Ordner + "\" + Texturname + ".png")
                    End If
                End If
            Else
                Dim Found As Boolean = False
                For Each File In My.Computer.FileSystem.GetFiles(DataSettings.Resourcenpfad + Ordner + "\Zoomed Out\", FileIO.SearchOption.SearchAllSubDirectories, "*.png")
                    If File.ToLower.EndsWith((Texturname + ".png").ToLower) Then
                        B = Image.FromFile(File)
                        Found = True
                    End If
                Next
                If Not Found Then
                    If My.Computer.FileSystem.FileExists(DataSettings.Resourcenpfad + Ordner + "\Zoomed Out\" + Texturname + ".png") Then
                        B = Image.FromFile(DataSettings.Resourcenpfad + Ordner + "\Zoomed Out\" + Texturname + ".png")
                    Else
                        B = Image.FromFile(DataSettings.Resourcenpfad + Ordner + "\" + Texturname + ".png")
                    End If
                End If
            End If
            EinzelBildGroessen.Width = B.Width / (Frames + 1)
            EinzelBildGroessen.Height = B.Height
            TexturBilde = Grafikengine.LoadTexture(B)
        End Sub
        Private Texturnamen As String
        Public ReadOnly Property Texturname() As String
            Get
                Return Texturnamen
            End Get
        End Property
        Private TexturBilde As Texture
        Public ReadOnly Property TexturBild() As Texture
            Get
                Return TexturBilde
            End Get
        End Property
        Public Shared Function GetTextur(ByVal Id As Integer) As Texture
            Return Texturen(Id).TexturBilde
        End Function
        Private EinzelBildGroessen As Size
        Public ReadOnly Property EinzelbildGroesse() As Size
            Get
                Return EinzelBildGroessen
            End Get
        End Property

    End Structure
    Protected BildID As Integer
    Protected FM As Boolean = True
    ''' <summary>
    ''' Naximale Bildzahl
    ''' </summary>
    ''' <remarks></remarks>
    Protected Frames As Integer
    ''' <summary>
    ''' Momentane Bildstelle
    ''' </summary>
    ''' <remarks></remarks>
    Protected Frame As Integer

    Protected Haltedauer As Integer = 1

    Private Counter As Integer = 0
    Public Property Count() As Integer
        Get
            Return Counter
        End Get
        Set(ByVal value As Integer)
            Counter += 1
            If Counter >= Haltedauer Then
                Counter = 0
            End If
        End Set
    End Property

    Public P As PointF
    Public Schlacht As Integer
    Protected InFeldern As Boolean = True
    Protected Function LoadTextur(ByVal Name As String, ByVal Frames As Integer, Optional ByVal Ordner As String = "Animations") As Integer
        For I = 0 To Texturen.Length - 1
            If Texturen(I).Texturname.ToUpper = Name.ToUpper Then
                Return I
            End If
        Next
        Array.Resize(Texturen, Texturen.Length + 1)
        Texturen(Texturen.Length - 1) = New Textur(Name, Ordner, Frames)
        Return Texturen.Length - 1
    End Function
    Public Overridable Function Malen(ByVal intColor As Color, Optional ByVal Zoomfaktor As Single = 1, Optional ByVal Feldbreite As Integer = -1) As Boolean
        If DataSettings.draw3D Then

        ElseIf DataSettings._2D_3D_Malen Then
            If Feldbreite <= 0 Then Feldbreite = Hauptmenue.Feldbreite
            Dim T As Textur = Texturen(BildID)
            Dim MittelPunkt As New PointF(T.EinzelbildGroesse.Width / 2, T.EinzelbildGroesse.Height / 2)

            Dim XPlus As Single = -MittelPunkt.X / Feldbreite
            Dim YPlus As Single = -(T.EinzelbildGroesse.Height - MittelPunkt.Y) / Feldbreite

            Dim Rechts As Single = (T.EinzelbildGroesse.Width) / Feldbreite
            Dim Oben As Single = (T.EinzelbildGroesse.Height) / Feldbreite
            Dim cube(3) As CustomVertex.PositionTextured

            If FM = True Then
                Dim FrameSize As Single = 1 / (Frames + 1)
                cube(0) = New CustomVertex.PositionTextured(0, 0, 0, FrameSize * Frame, 1)
                cube(1) = New CustomVertex.PositionTextured(0, 0, Oben * Zoomfaktor, FrameSize * Frame, 0)
                cube(2) = New CustomVertex.PositionTextured(Rechts * Zoomfaktor, 0, 0, FrameSize * Frame + FrameSize, 1)
                cube(3) = New CustomVertex.PositionTextured(Rechts * Zoomfaktor, 0, Oben * Zoomfaktor, FrameSize * Frame + FrameSize, 0)
            Else
                cube(0) = New CustomVertex.PositionTextured(0, 0, 0, 0, 1)
                cube(1) = New CustomVertex.PositionTextured(0, 0, Oben * Zoomfaktor, 0, 0)
                cube(2) = New CustomVertex.PositionTextured(Rechts * Zoomfaktor, 0, 0, 1, 1)
                cube(3) = New CustomVertex.PositionTextured(Rechts * Zoomfaktor, 0, Oben * Zoomfaktor, 1, 0)
            End If


            Dim Vec As Vector3
            If InFeldern Then
                Vec = ObjektSpielfeld.CalcDraw2D3DVec(CSng(P.X) + XPlus + 0.5, ObjektSpielfeld.SpielfeldLayer.Animationen, CSng(P.Y) - YPlus - 0.5)
            Else
                Vec = Grafikengine.GetEckPunktForBackground(P.X, P.Y, 0.2, Matrix.Identity)
            End If

            Grafikengine.malen2Din3D(Vec, cube, T.TexturBild, intColor)
        Else
            'Welchen Teil des Bildes wir malen wollen
            Dim sngRotation As Double = 0
            Dim T As Textur = Texturen(BildID)
            If FM = True Then
                With Grafikengine.recBild
                    .X = T.EinzelbildGroesse.Width * Frame : .Width = T.EinzelbildGroesse.Width
                    .Y = 0 : .Height = T.EinzelbildGroesse.Height
                End With
            Else
                With Grafikengine.recBild
                    .X = 0 : .Width = T.EinzelbildGroesse.Width
                    .Y = 0 : .Height = T.EinzelbildGroesse.Height
                End With
            End If
            With Grafikengine.ptfRotCenter
                .X = T.EinzelbildGroesse.Width / 2 * Zoomfaktor
                .Y = T.EinzelbildGroesse.Height / 2 * Zoomfaktor
            End With
            If InFeldern = True Then
                Grafikengine.ptfPosition = New Drawing.Point(Hauptmenue.Feldbreite * P.X + Objektverwalter.Spielfeld.BildschirmKooX, Hauptmenue.Feldbreite * P.Y + Objektverwalter.Spielfeld.BildschirmKooY)
            Else
                Grafikengine.ptfPosition = New Drawing.Point(P.X + Objektverwalter.Spielfeld.BildschirmKooX, P.Y + Objektverwalter.Spielfeld.BildschirmKooY)
            End If
            'Ausschnitte in den Backbuffer zeichnen
            'Wo wird das Bild gezeichnet
            '16 ist die Breite eines Feldes 8 die Haelfte das heisst wir ermitteln den Mittelpunkt des Feldes und ziehen die Haelfte der Groesse der Einheit ab
            '... und wie breit ist es
            With Grafikengine.szfScale
                .Height = T.EinzelbildGroesse.Height * Zoomfaktor
                .Width = T.EinzelbildGroesse.Width * Zoomfaktor
            End With
            'jetzt wird es in den Backbuffer gezeichnet
            Grafikengine.Sprite.Draw2D(T.TexturBild, Grafikengine.recBild, Grafikengine.szfScale, Grafikengine.ptfRotCenter, sngRotation, Grafikengine.ptfPosition, intColor)
        End If
        If Frame >= Frames Then
            Return False
        End If

        Count = 1
        If Count = 0 Then
            Frame += 1
        End If
        If Frame >= Frames Then
            Frame = Frames
        End If
        Return True
    End Function
    Public Sub New(ByVal Musikspielen As Boolean, ByVal Schlacht As Integer)
        Me.Schlacht = Schlacht
    End Sub
    Public Overridable Sub RemoveHandles()

    End Sub
    Public Shared Sub DeleteAnimationsTexturen()
        For i = 0 To Texturen.Length - 1
            Texturen(i).TexturBild.Dispose()
            Texturen(i) = Nothing
        Next
        ReDim Texturen(-1)
        Grafikengine.Grafikkarte.EvictManagedResources()
    End Sub
    Public Overridable Sub MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    End Sub
    Public Overridable Sub KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
    End Sub
End Class